package com.wzs.controller;

import com.google.gson.Gson;
import com.wzs.dao.impl.UserDao;
import com.wzs.model.User;
import com.wzs.model.UserCondition;
import com.wzs.service.IUserService;
import com.wzs.service.impl.UserService;
import com.wzs.util.PageModel;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
 * @author miaomiao
 * @version 1.0.0
 * @ClassName UserServlet.java
 * @Description TODO
 * @createTime 2021-06-19- 15:00:00
 */
public class UserServlet extends HttpServlet {

    private IUserService userService;

    public IUserService getUserService() {
        return userService;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    public void init() throws ServletException {
        UserDao ud = new UserDao();
        UserService us = new UserService();
        us.setUserDao(ud);
        userService = us;
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String operation = request.getParameter("operation");

        if ("getUserList".equalsIgnoreCase(operation)) {
            getUserList(request, response);
        } else if ("getUserDetail".equalsIgnoreCase(operation)) {
            getUserDetail(request, response);
        } else if ("getUserPage".equalsIgnoreCase(operation)) {
            getUserPage(request, response);
        } else if ("deleteUserById".equalsIgnoreCase(operation)) {
            deleteUserById(request, response);
        } else if ("saveOrUpdateUser".equalsIgnoreCase(operation)) {
            saveOrUpdateUser(request, response);
        }


    }

    private void saveOrUpdateUser(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String name = request.getParameter("fullname");
        String id = request.getParameter("id");
        String password = request.getParameter("password");


        User user = new User(id == null || id.equals("") ? null : Integer.valueOf(id), name, password, null);

        boolean rs = userService.saveOrUpdateUser(user);

        Gson g = new Gson();
        String json = g.toJson(rs);

        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        out.println(json);
        out.flush();
        out.close();

    }

    private void deleteUserById(HttpServletRequest request, HttpServletResponse response) throws IOException {

        System.out.println("执行删除用户记录操作");

        String uId = request.getParameter("uId");

        boolean rs = false;

        if (uId != null && !uId.equals("")) {
            rs = userService.deleteUserById(uId);
        }

        Gson g = new Gson();
        String json = g.toJson(rs);

        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        out.println(json);
        out.flush();
        out.close();


    }

    public void getUserPage(HttpServletRequest request, HttpServletResponse response) throws IOException {

        System.out.println("执行用户列表分页操作");

        //查询条件
        String name = request.getParameter("name");
        UserCondition condition = new UserCondition();
        condition.setName(name);

        //分页条件
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        PageModel<User> pageModel = new PageModel<>();
        pageModel.setPageNum(pageNum == null ? 1 : Integer.parseInt(pageNum));
        pageModel.setPageSize(pageSize == null ? 2 : Integer.parseInt(pageSize));

        PageModel<User> page = userService.getUserPageByCondition(condition, pageModel);

        Gson g = new Gson();
        String json = g.toJson(page);

        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        out.println(json);
        out.flush();
        out.close();

    }

    public void getUserDetail(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("进入到查看用户详情操作");

        String uId = request.getParameter("uid");
        Gson g = new Gson();
        String json = null;
        PrintWriter out = null;

        if (uId == null || uId.equals("")) {
            String msg = "用户编号不能为空";
            String m = g.toJson(msg);
            response.setContentType("application/json");
            try {
                out = response.getWriter();
                out.println(m);
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                out.close();
            }
        } else {
            User userDetails = userService.getUserDetails(uId);
            json = g.toJson(userDetails);
            response.setContentType("application/json");
            try {
                out = response.getWriter();
                out.println(json);
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                out.close();
            }
        }


    }

    public void getUserList(HttpServletRequest request, HttpServletResponse response) throws IOException {

        System.out.println("进入到查询操作所有用户操作");

        String name = request.getParameter("name");
        UserCondition condition = new UserCondition();

        condition.setName(name);

        ArrayList<User> userList = userService.getUserListByCondition(condition);

        Gson g = new Gson();
        String uList = g.toJson(userList);

        //System.out.println(uList);

        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        out.println(uList);
        out.flush();
        out.close();
    }


}
